package RobotPlayer
{
	import BasicEngine.Dispatcher;
	import BasicEngine.Vector2D;
	import DataStore.BmpDataPool;
	import flash.display.Bitmap;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	import GameLogic.Game;
	import SoundManager.SoundFactory;
	public class BaseMissile extends MoveEntity
	{
		public var MissileBody:Bitmap;
		public var MissileBodyIndex:Vector.<int>;
		public var WakeFlames:Bitmap;
		public var WakeFlameIndex:Vector.<int>
		public var MResultant:Resultant;
		public var Target:Vector2D = new Vector2D(400, 300);
		public var CdTime:int = 200;
		public var Sender:BaseRobot;
		public var RunLua:Boolean = false;
		public var SendAngle:Number;
		public var Effects:Vector.<BaseRobot>;
		
		private var timer:int = 0;
		private var mI:int = 0;
		private var fI:int = 0;
		private var copy:Sprite;
		private var ox:int = -6;
		private var oy:int = -10;
		
		public function BaseMissile(_owner:BaseRobot,_v:Vector2D, _mass:Number,_maxSpeed:Number,_maxForce:Number,
		                            _misslePos:Point,_wakeflame:Point,_a1:Array,_a2:Array)
		{
			Sender = _owner;
			super(_v, _mass, _maxSpeed, _maxForce);
			ID = Game.MissileNextId++;
			Canvas = new Sprite();
			copy = new Sprite();
			MissileBody = new Bitmap();
			WakeFlames = new Bitmap();
			copy.addChild(WakeFlames);
			copy.addChild(MissileBody);
			copy.x = ox;
			copy.y = oy;
			Canvas.addChild(copy);
			MissileBody.x = _misslePos.x;
			MissileBody.y = _misslePos.y;
			WakeFlames.x = _wakeflame.x;
			WakeFlames.y = _wakeflame.y;
			MissileBodyIndex = new Vector.<int>();
			WakeFlameIndex = new Vector.<int>();
			for each(var i:int in _a1)
			{
				MissileBodyIndex.push(i);
			}
			for each(var j:int in _a2)
			{
				WakeFlameIndex.push(j);
			}
			Canvas.addEventListener(Event.ENTER_FRAME, Update);
			
			MResultant = new Resultant(this);
			Effects = new Vector.<BaseRobot>();
			
			var shape:Shape = new Shape();
			shape.graphics.beginFill(0xff0000, 1);
			shape.graphics.drawCircle(0, 0, 1);
			Canvas.addChild(shape);
			Game.PlayerCanvas.addChild(Canvas);
			if(Game.SetButton.SettingPanel.turnoffSound.selected)
      			SoundFactory.Sounds["huojianfashe"].play();
			
			Game.AddMissileNum++;
		}
		
		private function Update(e:Event):void
		{
			if (Dispatcher.Clock.SystemTime - timer > 30)
			{
				//var force:Vector2D = MResultant.SendDirect(Target);
				var force:Vector2D = MResultant.SendAngle(SendAngle);
				
			    var acceleration:Vector2D = force.CrossProduct(1.0/Mass);
			    var t1:Vector2D = acceleration.CrossProduct(Dispatcher.Clock.Tick);
			    Velocity =  Velocity.Addition(t1); 			
			    var t2:Vector2D = Velocity.CrossProduct(Dispatcher.Clock.Tick);
			    t2.Turncate(MaxSpeed);
				
			    Pos = Pos.Addition(t2);	
				
				Canvas.x = Pos.X;
				Canvas.y = Pos.Y;
				
				if (Velocity.LengthSQ > 0.000001)
			    {
				   Heading = Velocity.NormalizeClone();
				   Side = Heading.Perp;
			    }
				
				//if (Math.abs(Pos.X - Target.X) < 10 && Math.abs(Pos.Y - Target.Y))
				//{
                    //var ex:BaseExplode = new BaseExplode(Game.PlayerCanvas, [113,114, 115, 116, 117, 118], 60, 1);
					//ex.x = Pos.X;
					//ex.y = Pos.Y;
					//Canvas.removeEventListener(Event.ENTER_FRAME, Update);
					//Game.PlayerCanvas.removeChild(Canvas);
				//}
				
			   Canvas.rotation = Heading.Angle * 180 / Math.PI + 90 ;
			   Heading.Angle += 0.1;
				
			   if (mI >= MissileBodyIndex.length) 
			   {
			     	mI = 0;
			   }
			   MissileBody.bitmapData = BmpDataPool.WeaPool[MissileBodyIndex[mI]];
			   mI++;
			   if (fI >= WakeFlameIndex.length)
			   {
			   	  fI = 0;
			   }
			   WakeFlames.bitmapData = BmpDataPool.WeaPool[WakeFlameIndex[fI]];
			   fI++;
			   timer = Dispatcher.Clock.SystemTime;
			   
			   
			   
			   for each(var item:BaseRobot in RobotFactory.RobotRecords)
			   {
				   if (item.ID != Sender.ID)
				     {
						 if (Math.abs(Pos.X - item.Pos.X) < 30 && Math.abs(Pos.Y - item.Pos.Y)<30)
				          {
                              var ex2:BaseExplode = new BaseExplode(Game.PlayerCanvas, [5012,5013,5014, 5015], 60, 1);
					          ex2.x = Pos.X;
					          ex2.y = Pos.Y;
							  //
							  Effects.push(item);
							  item.Hp -= 30;
					          Canvas.removeEventListener(Event.ENTER_FRAME, Update);
					          item.MissileExplode(this);
					          //Game.Pause = true;
				          }
					 }
			   }
			   if (Effects.length > 0)
			   {
				   Game.PlayerCanvas.removeChild(Canvas);
				   Game.RemovMissileNum--;
			   }
			   else  if (Pos.X > 756 || Pos.X < 0 || Pos.Y > 570 || Pos.Y < 0)
			   {
                    var ex3:BaseExplode = new BaseExplode(Game.PlayerCanvas, [5012,5013,5014], 60, 1);
					ex3.x = Pos.X;
					ex3.y = Pos.Y;
					Canvas.removeEventListener(Event.ENTER_FRAME, Update);
					Game.PlayerCanvas.removeChild(Canvas);
				    Game.RemovMissileNum--;
			   }
			}
		}
	}
}